/*
 * Copyright (C), 2013-2018, 上汽集团
 * FileName: WXJS.java
 * Author:   raolesong
 * Date:     2018年8月30日 上午10:15:48
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.jinznet.transit.wx;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinznet.transit.util.Constant;

/**
 * 〈功能详细描述〉
 * @author raolesong
 * 2018年8月30日 上午10:15:48
 */
public class WXJS {
	
	//获取accessToken
	public static JSONObject getAccessToken(){
	    String accessTokenUrl= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	    String requestUrl = accessTokenUrl.replace("APPID",Constant.APPID).replace("APPSECRET",Constant.APPID_SECRET);
	    System.out.println("getAccessToken.requestUrl====>"+requestUrl);
	    JSONObject result = HttpUtil.doGet(requestUrl);
	    System.out.println("getAccessToken result====>"+JSON.toJSONString(result));
	    return result ;
	}

	//获取ticket
	public static JSONObject getJsApiTicket(){
		String accessToken = getAccessToken().getString("access_token");
	    String apiTicketUrl= "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
	    String requestUrl = apiTicketUrl.replace("ACCESS_TOKEN", accessToken);
	    JSONObject result = HttpUtil.doGet(requestUrl);
	    System.out.println("getJsApiTicket result====>"+JSON.toJSONString(result));
	    return result;
	}

	//生成微信权限验证的参数
	public static Map<String, String> makeWXTicket(String jsApiTicket, String url) {
		System.out.println("加密的url==="+url);
	    Map<String, String> ret = new HashMap<String, String>();
	    String nonceStr = createNonceStr();
	    String timestamp = createTimestamp();
	    String string1;
	    String signature = "";

	    //注意这里参数名必须全部小写，且必须有序
	    string1 = "jsapi_ticket=" + jsApiTicket +
	            "&noncestr=" + nonceStr +
	            "&timestamp=" + timestamp +
	            "&url=" + url;
	    try
	    {
	        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
	        crypt.reset();
	        crypt.update(string1.getBytes("UTF-8"));
	        signature = byteToHex(crypt.digest());
	        System.out.println("signature=====>"+signature);
	    }
	    catch (Exception e)
	    {
	    	e.printStackTrace();
	    }

	    ret.put("url", url);
	    ret.put("jsapi_ticket", jsApiTicket);
	    ret.put("nonceStr", nonceStr);
	    ret.put("timestamp", timestamp);
	    ret.put("signature", signature);
	    ret.put("appid", Constant.APPID);
	    return ret;
	}
	//字节数组转换为十六进制字符串
	private static String byteToHex(final byte[] hash) {
	    Formatter formatter = new Formatter();
	    for (byte b : hash)
	    {
	        formatter.format("%02x", b);
	    }
	    String result = formatter.toString();
	    formatter.close();
	    return result;
	}
	//生成随机字符串
	private static String createNonceStr() {
	    return UUID.randomUUID().toString();
	}
	//生成时间戳
	private static String createTimestamp() {
	    return Long.toString(System.currentTimeMillis() / 1000);
	}


}
